import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login.vue'),
    meta:{
      title:"用户登录"
    }
  },
  {
    path: '/',
    component: () => import('../views/Home.vue'),
    children:[
      {
        path: '',
        redirect:"/home/index"
      },
      {
        path: '/home/index',
        name: 'HomeIndex',
        component: () => import('../views/Home/index'),
        meta:{
          title:"首页",
          requireAuth: true,
        }
      },
      {
        path: '/home/bestseller',
        name: 'BestSeller',
        component: () => import('../views/Home/BestSeller'),
        meta:{
          title:"畅销图书",
          requireAuth: true,
        }
      },
      {
        path: '/home/contactservice',
        name: 'ContactService',
        component: () => import('../views/Home/ContactService'),
        meta:{
          title:"联系客服",
          requireAuth: true,
        }
      },
      {
        path: '/home/opinion',
        name: 'Opinion',
        component: () => import('../views/Home/Opinion'),
        meta:{
          title:"意见反馈",
          requireAuth: true,
        }
      },
      {
        path: '/personal',
        name: 'PersonalPage',
        component: () => import('../views/PersonalPage.vue'),
        meta:{
          title:"个人主页",
          requireAuth: true,
        }
      },
    ],
    meta:{
      title:"众众图书商城",
      requireAuth: true,
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register.vue'),
    meta:{
      title:"用户注册"
    }
  },
  {
    path: '/bookdetails/:isbn',
    name: 'BookDetails',
    component: () => import('../views/BookDetails.vue'),
    meta:{
      title:"书籍详情页",
      requireAuth: true,
    }
  },
  {
    path: '/blankpage',
    name: 'BlankPage',
    component: () => import('../views/BlankPage.vue'),
    meta:{
      title:"加载中...",
      requireAuth: true,
    }
  },
  //  404页面，
  {
    path: '/404',
    name: 'Error',
    component: ()=> import('../views/Error'),
    meta:{
      title:"页面出错啦!",
    },
  },
  // 所有未定义路由，全部重定向到404页面
  {
    path:"*",//匹配所有路由
    redirect:"/404",//重定向
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});
router.beforeEach(function (to,form,next) {
  // console.log(to);
  // console.log(form);

  document.title=to.meta.title;
  next();
});

export default router
